//
//  RHDeviceSystemVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/4.
//

#import "RHDeviceSystemVC.h"
#import "RHDeviceTopoCell.h"
#import "RHEfficiencyRankCell.h"
#import "RHEnergyStorageStatusCell.h"
#import "RHDeviceSetCell.h"
#import "RHFirmwareListVC.h"
#import "RHDeviceDataModel.h"
#import "RHAlarmFaultListVC.h"
#import "RHDeviceInfoController.h"
#import "RHConfigOptionVC.h"
#import "RHDeviceParaSetVC.h"
#import "RHDeviceToPoFullScreenVC.h"
#import "RHFirmwareModel.h"
#import "RHMEDataMonitorVC.h"
#import "RHDevEmsListVC.h"
#import "RHDevEndPointDataModel.h"
#import "RHTopoModel.h"
#import "RH233TopoModel.h"
#import "RHDeviceBatteryVC.h"
#import "RHEndPointTypeModel.h"
#import "RHEndPointModel.h"
#import "RHMeDeviceEndpointCell.h"
#import "RHDeviceDetailModel.h"
#import "RHMeEndpointTypeModel.h"

@interface RHDeviceSystemVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray *titleArr;

@property (nonatomic ,strong) UIView *headerView;
//能效排行
@property (nonatomic ,strong) NSArray *efficiencyRankArr;

@property (nonatomic ,strong) QMUILabel *alarmLab;

@property (nonatomic ,strong) QMUILabel *faultLab;

@property (nonatomic ,strong) NSDictionary *deviceInfo;
//topo数据 储能状态
@property (nonatomic ,strong) RHTopoModel *topoModel;
//233k  topo数据
@property (nonatomic ,strong) RH233TopoModel *topo233Model;

@property (nonatomic, copy) NSString *emuSN;

@property (nonatomic ,strong) UIView *pointView;

@property (nonatomic ,strong) RHDeviceDetailModel *infoModel;
@end

@implementation RHDeviceSystemVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getData];
}

-(void)getData
{
   
    [self getDeviceInfoFromPlant];
    [self getEnergyEfficiencyRank];
    [self getEndPointReration];
    [self getDeviceEndpointInfo];
    if ([self.deviceModel.deviceTypeCode is233DeviceType]) {
        [self getDeviceTopo233];
        
    }else{
        [self getDeviceInfo];
    }
}

- (void)setupUI{
    kWeakSelf;
    self.titleArr = @[@"能量流向".rh_localized,@"供载率".rh_localized];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = CFTabViewBgColor;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHDeviceTopoCell class] forCellReuseIdentifier:DevTopoCellHEBat];
    [_tableView registerClass:[RHDeviceTopoCell class] forCellReuseIdentifier:DevTopoCellHEBat_Parallel];
    [_tableView registerClass:[RHDeviceTopoCell class] forCellReuseIdentifier:DevTopoCellME];
    [_tableView registerClass:[RHDeviceTopoCell class] forCellReuseIdentifier:TopoME_233EUCell];
    [_tableView registerClass:[RHDeviceTopoCell class] forCellReuseIdentifier:DevTopoCellME_61];
    [_tableView registerClass:[RHDeviceTopoCell class] forCellReuseIdentifier:TopoME_215EUCell];
    [_tableView registerClass:[RHEfficiencyRankCell class] forCellReuseIdentifier:@"RHEfficiencyRankCell"];
    [_tableView registerClass:[RHEnergyStorageStatusCell class] forCellReuseIdentifier:@"PEDeviceStatusCell01"];
    [_tableView registerClass:[RHEnergyStorageStatusCell class] forCellReuseIdentifier:@"PEDeviceStatusCell_233"];
    [_tableView registerClass:[RHDeviceSetCell class] forCellReuseIdentifier:@"RHDeviceSetCell"];
    [_tableView registerClass:[RHMeDeviceEndpointCell class] forCellReuseIdentifier:@"RHMeDeviceEndpointCell"];
    _tableView.tableHeaderView = self.headerView;
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [_tableView addHeaderRefresh:^{
        [weakSelf getData];
    }];
}

#pragma mark ---- 设备告警
- (void)alarmAction{
    RHAlarmFaultListVC *vc = [[RHAlarmFaultListVC alloc] initWithLevel:1 deviceId:self.deviceModel.deviceId];
    vc.devModel = self.deviceModel;
    vc.showRepairBtn = YES;
    [vc.navigationItem setTitle:@"告警".rh_localized];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ---- 设备故障
- (void)faultAction{
    RHAlarmFaultListVC *vc = [[RHAlarmFaultListVC alloc] initWithLevel:2 deviceId:self.deviceModel.deviceId];
    vc.devModel = self.deviceModel;
    vc.showRepairBtn = YES;
    [vc.navigationItem setTitle:@"故障".rh_localized];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----- 点击端点
-(void)clickEndpoint:(RHMeEndpointTypeModel*)endPointTypeModel
{
   
        RHDeviceInfoController *deviceInfoVC = [[RHDeviceInfoController alloc]initWithDevModel:self.deviceModel endpointName:endPointTypeModel.endpointTypeCode];
       
        [self.navigationController pushViewController:deviceInfoVC animated:YES];
    
}

#pragma mark ------ 获取电站下的设备信息  故障和告警 数量
-(void)getDeviceInfoFromPlant
{
    [QMUITips showLoadingInView:self.view];
    [RHDeviceDataModel startGetWithCompletionAPIKey:H_getDeviceData argument:@{@"deviceId":self.deviceModel.deviceId} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            RHDeviceDataModel *dataModel = resultModel.data;
            self.faultLab.text = [NSString stringWithFormat:@"%ld",dataModel.fault];
            self.alarmLab.text = [NSString stringWithFormat:@"%ld",dataModel.alarm];
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
        
    }];
    
}

#pragma mark -----1. 获取设备端点关系 -------
- (void)getEndPointReration{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@/%@",H_getDevRelationTypeList,self.deviceModel.deviceId];
    [RHEndPointTypeModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
        
            NSArray *typeArr = resultModel.data;
            
            
            [typeArr enumerateObjectsUsingBlock:^(RHEndPointTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
              
                //麦格瑞能
                if ([obj.endpointTypeCode isEqualToString:@"InvMegarevoLv"]&&obj.nodes.count>0) {
                    
                    [self getIverterType:obj];
                    *stop = YES;
                }
                
                
                
            }];
            
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
    
    
}

#pragma mark ------- 2.获取逆变器类型 为日月元而设---------
-(void)getIverterType:(RHEndPointTypeModel*)endPointTypeModel
{
    if (endPointTypeModel.nodes.count==0) {
        return;
    }
    RHEndPointModel *endpointModel = [endPointTypeModel.nodes firstObject];
    [QMUITips showLoadingInView:self.view];
    [BaseModel startGetWithCompletionAPIKey:H_getParamStatusData argument:@{@"deviceId":ValidNullData(self.deviceModel.deviceId),@"sn":ValidNullData(endpointModel.sn)} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        if(resultModel.code == 200){
            self.deviceModel.inverterType = (DevInverterType)[request.responseJSONObject[@"data"][@"Inv_DevType"] integerValue];
            if (self.deviceModel.inverterType == DevInverterTypeMGRN7K||
                self.deviceModel.inverterType == DevInverterTypeMGRN12K) {
                
                [self.tableView reloadData];
            }

        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        
    }];
}
#pragma mark ---- 数据监控
-(void)dataMonitorAction
{
//    if(self.deviceModel.productTypeId == ProductType_ME){
//        RHMEDataMonitorVC *monitorVC = [[RHMEDataMonitorVC alloc]init];
//        monitorVC.deviceModel = self.deviceModel;
//        [self.navigationController pushViewController:monitorVC animated:YES];
//    }
//    if (self.deviceModel.productTypeId == ProductType_HE_BAT||
//        self.deviceModel.productTypeId == ProductType_HE_ALLINONE) {
        RHDeviceInfoController *deviceInfoVC = [[RHDeviceInfoController alloc]init];
        deviceInfoVC.deviceModel = self.deviceModel;
        [self.navigationController pushViewController:deviceInfoVC animated:YES];
//    }
}

#pragma mark ----- 运行配置
-(void)deviceConfigAction
{
    RHConfigOptionVC *configVC = [[RHConfigOptionVC alloc]init];
    configVC.deviceModel = self.deviceModel;
    [self.navigationController pushViewController:configVC animated:YES];
}

#pragma mark ----- 固件升级
-(void)deviceFirewareUgradeAction
{
    if([self.deviceInfo[@"upgrade"] boolValue] == false){
        [QMUITips showInfo:@"暂无可升级的固件".rh_localized];
        return;
    }
    RHFirmwareListVC *vc = [[RHFirmwareListVC alloc]init];
    vc.deviceModel = self.deviceModel;
    [self.navigationController pushViewController:vc animated:YES];
}

////储能状态
//-(void)getStorageStatus
//{
//    [QMUITips showLoadingInView:self.view];
//    [BaseModel startGetWithCompletionAPIKey:H_getStorageStatus argument:@{@"deviceId":self.deviceModel.deviceId} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
//        if(resultModel.code == 200){
//            self.storageDic = request.responseJSONObject[@"data"];
//            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:2];
//            [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
//        }
//    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
//            
//    }];
//}

//能效排行
-(void)getEnergyEfficiencyRank
{
    [QMUITips showLoadingInView:self.view];
    [BaseModel startGetWithCompletionAPIKey:H_getEnergyRanking_v1 argument:@{@"deviceId":self.deviceModel.deviceId} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        
        NSArray *typeArr = @[@"电池".rh_localized,@"电网".rh_localized,@"光伏".rh_localized];
        NSArray *keyArr = @[@"battery",@"grid",@"photovoltaic"];
        NSMutableArray *mtbArr = [NSMutableArray array];
        for (int i = 0 ; i < 3; i++) {
            NSMutableDictionary *mtbDic = [NSMutableDictionary dictionary];
            [mtbDic setObject:typeArr[i] forKey:@"productTypeCode"];
            [mtbDic setObject:ValidNullDataToString(request.responseJSONObject[@"data"][keyArr[i]], @"--") forKey:@"value"];
            [mtbArr addObject:mtbDic];
        }
        self.efficiencyRankArr = mtbArr.copy;
        [self.tableView reloadData];
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
}

#pragma mark ------ 获取设备部件信息
-(void)getDeviceEndpointInfo
{
 
    NSDictionary *para = @{
        @"deviceId":self.deviceModel.deviceId
    };
    [RHDeviceDetailModel startGetWithCompletionAPIKey:H_MeDeviceMonitorDetail argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        if (resultModel.code == 200) {
            self.infoModel = resultModel.data;
            [self.tableView reloadData];
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
}
#pragma mark ------ 获取设备信息(topo数据,有无可升级固件,储能状态)
-(void)getDeviceInfo
{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@%ld",H_getDeviceInfo,self.deviceModel.device_id];
    [RHTopoModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            self.topoModel = resultModel.data;
            self.deviceInfo = request.responseJSONObject[@"data"];
            self.pointView.hidden = ![self.deviceInfo[@"upgrade"] boolValue];
            self.deviceModel.status = self.deviceInfo[@"status"];
            self.deviceModel.invState = [self.deviceInfo[@"invState"] integerValue];
            NSString *devName = ValidNullData(self.deviceInfo[@"deviceName"]);
            if (self.updateDevInfoBlock) {
                self.updateDevInfoBlock(self.deviceInfo[@"status"],devName);
            }
            [self.tableView reloadData];
            [self.tableView endHeaderRefresh];
        }
        
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
                
        }];
}

#pragma mark ------ 获取设备topo信息(233k)
-(void)getDeviceTopo233
{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@%ld",H_getDeviceTopoInfo233,self.deviceModel.device_id];
    [RH233TopoModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            self.topo233Model = resultModel.data;
            [self.tableView reloadData];
            [self.tableView endHeaderRefresh];
        }
        
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
                
        }];
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *headView = kView;
//    headView.backgroundColor = CFTabViewBgColor;
//    UILabel *titleLab = [[QMUILabel alloc]init];
//    titleLab.font = kBOLDSYSTEMFONT(16);
//    titleLab.textColor = kColorRGB(0, 0, 0);
//    titleLab.textAlignment = NSTextAlignmentLeft;
//    titleLab.text = self.titleArr[section];
//    [headView addSubview:titleLab];
//    
//    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(headView).offset(15);
//        make.bottom.mas_offset(-10);
//     }];
    
   return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    kWeakSelf;
    if(indexPath.section == 0){
        kWeakSelf;
        //部件图
        RHMeDeviceEndpointCell *endpointCell = [tableView dequeueReusableCellWithIdentifier:@"RHMeDeviceEndpointCell"];
        
        endpointCell.infoModel = self.infoModel;
        
        endpointCell.clickDeviceBlock = ^(RHMeEndpointTypeModel * _Nonnull endpointModel) {
            [weakSelf clickEndpoint:endpointModel];
        };
        endpointCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return endpointCell;
        
        //能量流向
        RHDeviceTopoCell *cell ;
        if(self.deviceModel.productTypeId == ProductType_ME){
            if ([self.deviceModel.deviceTypeCode is61DeviceType]) {
                cell = [tableView dequeueReusableCellWithIdentifier:DevTopoCellME_61];
                [cell setTopoModel:self.topoModel deviceModel:self.deviceModel];
            }else if ([self.deviceModel.deviceTypeCode isEqualToString:K_KS215ACB01_EU]) {
                cell = [tableView dequeueReusableCellWithIdentifier:TopoME_215EUCell];
                [cell setTopoModel:self.topoModel deviceModel:self.deviceModel];
            }else if([self.deviceModel.deviceTypeCode isEqualToString:R_SM233120A0_EU]||
                     [self.deviceModel.deviceTypeCode isEqualToString:K_KS233125A0_EU]){
                cell = [tableView dequeueReusableCellWithIdentifier:TopoME_233EUCell];
                [cell set233TopoModel:self.topo233Model deviceModel:self.deviceModel];
            }else{
                cell = [tableView dequeueReusableCellWithIdentifier:DevTopoCellME];
                [cell set233TopoModel:self.topo233Model deviceModel:self.deviceModel];
            }
            
        }else if(self.deviceModel.productTypeId == ProductType_HE_BAT||
                 self.deviceModel.productTypeId == ProductType_HE_ALLINONE){
//            if (self.topoModel.batteryList.count>1) {
                cell = [tableView dequeueReusableCellWithIdentifier:DevTopoCellHEBat_Parallel];
//            }else{
//                cell = [tableView dequeueReusableCellWithIdentifier:DevTopoCellHEBat];
//            }
            
            [cell setTopoModel:self.topoModel deviceModel:self.deviceModel];
        }else{
            return [UITableViewCell new];
        }
       
        //全屏
        cell.fullScreenAction = ^{
            RHDeviceToPoFullScreenVC *fullVC = [[RHDeviceToPoFullScreenVC alloc]initWithDevModel:weakSelf.deviceModel topoModel:weakSelf.topoModel emuSN:weakSelf.emuSN ];
           
            [weakSelf.navigationController pushViewController:fullVC animated:NO];
        };
        return cell;
    }else if(indexPath.section == 1){
        //供载率
        RHEfficiencyRankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHEfficiencyRankCell"];
        cell.efficiencyRankArr = self.efficiencyRankArr;
        return cell;
    }
//    else if (indexPath.section == 2){
//        //储能状态  ,已取消,放到了数据监控pack顶部
//        RHEnergyStorageStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PEDeviceStatusCell01"];
//        if(self.deviceModel.productTypeId == ProductType_ME){
//            cell = [tableView dequeueReusableCellWithIdentifier:@"PEDeviceStatusCell_233"];
//        }
//        
//        cell.topoModel = self.topoModel;
//        kWeakSelf;
//        cell.detailBlock = ^{
//            if(weakSelf.deviceModel.productTypeId == ProductType_HE_BAT||
//               weakSelf.deviceModel.productTypeId == ProductType_HE_ALLINONE||
//               weakSelf.deviceModel.productTypeId == ProductType_ME){
//                RHDeviceBatteryVC *batteryVC = [[RHDeviceBatteryVC alloc]init];
//                batteryVC.devModel = weakSelf.deviceModel;
//                [weakSelf.navigationController pushViewController:batteryVC animated:YES];
//                return;
//            }
//
//        };
//        return cell;
//    }
    return [UITableViewCell new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
  
}


-(UIView*)headerView
{
    if(!_headerView){
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, kScaleW(65)+30+10 + 115)];
        _headerView.backgroundColor = CFTabViewBgColor;
        kWeakSelf;
        UIView *bgView = [[UIView alloc] init];
        bgView.layer.cornerRadius = 8;
        bgView.layer.masksToBounds = YES;
        [bgView setBackgroundColor:CFTabViewBgColor];
        [_headerView addSubview:bgView];
        
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.top.mas_offset(10);
            make.right.mas_offset(-15);
            make.height.mas_offset(100);
        }];
        
        __block UIView *lastView;
        __block CGFloat itemW = (kUI_WINDOW_W-30-10)/2;
        
        NSArray *titles = @[@"故障".rh_localized,@"告警".rh_localized];
        NSArray *images = @[@"kwt_device_alarm",@"kwt_device_fault"];
        [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIView *itemView = [weakSelf itemViewWithIndex:idx title:titles[idx] image:images[idx]];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
            [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
                if(idx == 0){
                    [weakSelf faultAction];
                }else{
                    [weakSelf alarmAction];
                }
            }];
            [itemView addGestureRecognizer:tap];

            [bgView addSubview:itemView];
            if(idx == 0){
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(0);
                    make.top.mas_offset(10);
                    make.width.offset(itemW);
                    make.bottom.mas_offset(-10);
                }];
            }else{
               
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(10);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
                
            }
                    
            lastView = itemView;
        }];
        
        //数据监控  运行配置  固件升级
        UIView *bot_bgView = kView;
        bot_bgView.backgroundColor = UIColor.whiteColor;
        [bot_bgView rounded:8 width:1 color:kBorderColor];
        [_headerView addSubview:bot_bgView];
        
        [bot_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView.mas_bottom).offset(10);
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.height.mas_offset(115);
        }];
        CGFloat itemWidth = (kUI_WINDOW_W - 30)/3;
        NSArray *bot_titles = @[@"数据监控".rh_localized,@"运行配置".rh_localized,@"固件升级".rh_localized];
        NSArray *bot_images = @[@"kwt_device_monitor",@"kwt_device_config",@"kwt_device_ugrade"];
        NSArray *actions = @[@"dataMonitorAction",@"deviceConfigAction",@"deviceFirewareUgradeAction"];
        [bot_titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
            UIView *itemView = kView;
            [bot_bgView addSubview:itemView];
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(idx * itemWidth);
                make.top.bottom.mas_offset(0);
                make.width.offset(itemWidth);
            }];
            
            UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:bot_images[idx]]];
            [itemView addSubview:icon];
            [icon mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.offset(50);
                make.top.mas_offset(15);
                make.centerX.mas_offset(0);
            }];
            
            if (idx == 2) {
                UIView *pointView = kView;
                pointView.backgroundColor = kCOLOR_HEX(@"FA5555");
                [icon addSubview:pointView];
                pointView.hidden = YES;
                self.pointView = pointView;
                pointView.layer.cornerRadius = 4;
                [pointView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.height.offset(8);
                    make.right.mas_offset(-3);
                    make.top.mas_offset(3);
                }];
            }
           
            
            QMUILabel *titleLab = [QMUILabel labelWithText:bot_titles[idx] font:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#171A1D")];
            titleLab.numberOfLines = 2;
            [itemView addSubview:titleLab];
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(5);
                make.right.mas_offset(-5);
                make.top.equalTo(icon.mas_bottom).offset(10);
            }];
            
            QMUIButton *btn = [[QMUIButton alloc]init];
            [itemView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.insets(UIEdgeInsetsZero);
            }];
            
            [[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
                SEL selector = NSSelectorFromString(actions[idx]);
                if ([weakSelf respondsToSelector:selector]) {
                    [weakSelf performSelector:selector];
                }
            }];
        }];
    }
    return _headerView;
}

//故障告警
-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr
{
    UIView *bgView = kView;
    [bgView rounded:8 width:1 color:kBorderColor];
    bgView.backgroundColor = UIColor.whiteColor;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_2"]];
    [bgView addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.width.height.offset(15);
            make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.text = [NSString stringWithFormat:@"%@",title];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    titleLab.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    [bgView addSubview:contentLab];
    
    if(idx ==0){
        self.faultLab = contentLab;
    }else{
        self.alarmLab = contentLab;
    }
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(10);
        make.right.equalTo(arrow.mas_left).offset(-3);
        make.centerY.mas_offset(-12);
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.centerY.mas_offset(12);
    }];
    
    
    return bgView;
}

-(UIView *)listView
{
    return self.view;
}


@end
